package boot.service.impl;

import boot.dao.UserDao;
import boot.domain.Role;
import boot.domain.User;
import boot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
    @Autowired
    UserDao userDao;

    public List<Role> getRoles(String name){
        List<Role> list = userDao.findRolesByName(name);
        System.out.println(list);
        return list;
    }

    @Cacheable(cacheNames = "user",key = "#name",condition = "#name!=null")
    public User getUser(String name){
        return this.getById(name);
    }

    @CacheEvict(cacheNames = "user",key = "#userName")
    public void deleteUser(String userName) {
       this.removeById(userName);
    }

    @CacheEvict(cacheNames = "user",key = "#name")
    public boolean updateUser(String name, User user) {
        return this.saveOrUpdate(user);
    }
}
